<%@page import="java.security.MessageDigest"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="conectar.Conectar"%>
<%@page session="true" %>
<%
    String usuario = "";
    String clave = "";
    
    if (request.getParameter("usuario") != null) 
            {
                usuario = request.getParameter("usuario");
            }
    
    if (request.getParameter("clave") != null) 
            {
                clave = request.getParameter("clave");
            }
   
    
     // -----------------------INICIO MD5 ------------------------------
    
        MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
        mdAlgorithm.update(clave.getBytes());

        byte[] digest = mdAlgorithm.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < digest.length; i++)
        
            {
                clave = Integer.toHexString(0xFF & digest[i]);

                if (clave.length() < 2) 
                    {
                        clave = "0" + clave;
                    }

                hexString.append(clave);
            }

        clave= hexString.toString();
        
     // -----------------------FIN MD5 ------------------------------
        
        
    try {
        conectar.Conectar miconexion = new Conectar();
        String sql = "SELECT *  FROM `Votante` WHERE `PersonaIFE` = " + usuario + " AND `password` = '" + clave + "'"; //el que sirve el de abajo es ajuste
        //String sql = "SELECT *  FROM `votante` WHERE `IFE` = " + usuario + " AND `password` = '" + md5 + "'";
        ResultSet consulta = miconexion.consulta(sql);
        
        if (consulta.next()) 
        
            {
            if (usuario.equals(consulta.getString(1)) && clave.equals(consulta.getString(2))) 
                {
                    HttpSession sesionOk = request.getSession();
                    sesionOk.setAttribute("usuario", usuario);
                    %>
                    <jsp:forward page="menu.jsp" />
                    <%

                } 
            
            else 
                {
                    %>
                        <jsp:forward page="login.jsp">
                        <jsp:param name="error" value="Usuario y/o clave incorrectos.<br>Vuelve a intentarlo."/>
                        </jsp:forward>
                    <%    
                }
            }
        
        miconexion.desconectar();
    
        } 
    
        catch (Exception e) 
            {
                out.println(e.getMessage());
            }


%>